/*
 * main.cc
 *
 *  Created on: Nov 10, 2010
 *      Author: fify
 */

/*
 * This program should be a DFS search. All we need is a solution that meets the requirement.
 */
#include <iostream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;

class Cell
{
public:
	int value;

	vector<int> candidate;

	int row, col;

public:
	int remove(int num)
	{
		vector<int>::iterator it;
		for(it = candidate.begin();it != candidate.end();it++)
		{
			if(*it == num)
			{
				break;
			}
		}

		if(it != candidate.end())
		{
			candidate.erase(it);
			return num;
		}

		return -1;
	}

	bool operator<(const Cell &cell) const
	{
		return candidate.size() < cell.candidate.size();
	}
};

Cell cell[9][9];
int mp[9][9];

int check(int row, int col, int num)
{
	for(int i = 0;i<9;i++)
	{
		if(mp[i][col] == num || mp[row][i] == num)
		{
			return 0;
		}
	}

	for(int i = row / 3 * 3;i<row / 3 * 3 + 3;i++)
	{
		for(int j = col / 3 * 3; j < col / 3 * 3 + 3;j++)
		{
			if(mp[i][j] == num)
				return 0;
		}
	}

	return 1;
}

int input(int count)
{
	Cell *p = (Cell *)cell;

	if(count >= 9 * 9)
	{
		return 1;
	}

	if(p[count].value >= 0)
	{
		return input(count + 1);
	}
	else
	{
		vector<int>::iterator it;
		for(it = p[count].candidate.begin(); it != p[count].candidate.end();++it)
		{
			if(check(p[count].row, p[count].col, *it) == 0)
			{
				continue;
			}

			mp[p[count].row][p[count].col] = *it;

			if(input(count + 1))
			{
				return 1;
			}
		}

		mp[p[count].row][p[count].col] = 0;

		return 0;
	}
}

void rm_candidate(int row, int col, int num)
{
	for(int i = 0;i<9;i++)
	{
		cell[i][col].remove(num);
		cell[row][i].remove(num);
	}

	for(int i = row / 3 * 3;i<row / 3 * 3 + 3;i++)
	{
		for(int j = col / 3 * 3; j < col / 3 * 3 + 3;j++)
		{
			cell[i][j].remove(num);
		}
	}

	cell[row][col].value = num;
	cell[row][col].candidate.clear();
}

void init()
{
	for(int i = 0;i<9;i++)
	{
		getchar();
		for(int j = 0;j<9;j++)
		{
			scanf("%c", &mp[i][j]);
			mp[i][j] -= '0';
		}
	}
	for(int i = 0;i<9;i++)
	{
		for(int j = 0;j<9;j++)
		{
			cell[i][j].candidate.clear();
			cell[i][j].row = i;
			cell[i][j].col = j;
			cell[i][j].value = -1;

			for(int k = 1;k<=9;k++)
			{
				cell[i][j].candidate.push_back(k);
			}
		}
	}
	for(int i = 0;i<9;i++)
	{
		for(int j = 0;j<9;j++)
		{
			if(mp[i][j])
			{
				rm_candidate(i, j, mp[i][j]);
			}
		}
	}

	sort((Cell *)cell, (Cell *)cell + 9 * 9);
}

void output()
{
	for(int i = 0;i<9;i++)
	{
		for(int j = 0;j<9;j++)
		{
			printf("%d", mp[i][j]);
		}
		printf("\n");
	}
}

int main()
{
	int n;
	scanf("%d", &n);

	int ans;
	for(int i = 0;i<n;i++)
	{
		init();

		ans = input(0);

		output();
	}
}
